/*
 * Famon (http://code.google.com/p/famon/)
 * Copyright (c) 2010 Carl Crous.
 *
 * This file is part of Famon.
 *
 * Famon is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Famon is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Famon.  If not, see <http://www.gnu.org/licenses/>.
 */

package za.org.foobar.famon.model;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Text;
import za.org.foobar.famon.util.DB;

import javax.jdo.annotations.*;

/**
 *
 */
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class ApplicationUser extends ModelObject {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key id;

    @Persistent
    private Application application;

    @Persistent
    private Key userId;
    @NotPersistent
    private UserData user;

    @Persistent
    private int role;
    @Persistent
    private Text alertRegex;

    public ApplicationUser() {
    }

    public ApplicationUser(Application application, Key userId, int role) {
        this.application = application;
        this.userId = userId;
        this.role = role;
    }

    public Key getId() {
        return id;
    }

    public void setId(Key id) {
        this.id = id;
    }

    public Application getApplication() {
        return application;
    }

    public void setApplication(Application application) {
        this.application = application;
    }

    public Key getUserId() {
        return userId;
    }

    public void setUserId(Key userId) {
        this.userId = userId;
    }

    public UserData getUser() {
        if (user == null) {
            if (userId == null) {
                return null;
            }
            user = DB.find(UserData.class, userId);
        }
        return user;
    }

    public void setUser(UserData user) {
        userId = user.getId();
        this.user = user;
    }

    public int getRole() {
        return role;
    }

    public void setRole(int role) {
        this.role = role;
    }

    public String getAlertRegex() {
        return alertRegex != null ? alertRegex.getValue() : null;
    }

    public void setAlertRegex(String alertRegex) {
        this.alertRegex = alertRegex != null ? new Text(alertRegex) : null;
    }

    public String keyToString() {
        return keyToString(id);
    }

    public static String keyToString(Key id) {
        return Long.toString(id.getId());
    }

    public static Key stringToKey(Key appId, String id) {
        return KeyFactory.createKey(appId, ApplicationUser.class.getSimpleName(), Long.parseLong(id));
    }
}
